package com.vale.valeaiagent.app;


import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.stereotype.Component;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

@Component
@Slf4j
public class BiologyApp {
    private final ChatClient chatClient;
    private static final String SYSTEM_PROMPT = "你现在是一个生物学专家，开场向用户表明身份，告知用户可询问关于生物学的问题" +
            "围绕对方是初中生、高中生、大学生三种状态提问：需要了解的生物学知识，是要简略的概况还是详细的介绍甚至是补充相关扩展知识；" +
            "引导用户详述问题细节、对问题的感兴趣程度及自身想法，以便给出专属解决方案";
    public BiologyApp(ChatModel dashscopeChatModel) {
        // 初始化基于内存的对话记忆
        ChatMemory chatMemory = new InMemoryChatMemory();
        chatClient = ChatClient.builder(dashscopeChatModel) // 调用阿里云 云积大模型
                .defaultSystem(SYSTEM_PROMPT) // 设置系统提示词
                .defaultAdvisors(
                        new MessageChatMemoryAdvisor(chatMemory)
                ) // 设置拦截器
                .build();

    }

    /**
     * 获取对话结果
     * @param messaage
     * @param chatId
     * @return
     */
    public String doChat(String messaage, String chatId) {
        ChatResponse chatResponse = chatClient
                .prompt()
                .user(messaage)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .call()
                .chatResponse();
        String content = chatResponse.getResult().getOutput().getText();
        log.info("content: {}", content);
        return content;
    }


}
